package jpb.module.dao.department;

import jpb.module.dao.department.mapper.DepartmentMapper;
import jpb.module.daomain.department.Department;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * Created by haol on 2017/4/5.
 */
@Repository
public class DepartmentDao {
        @Autowired
        private DepartmentMapper mapper;

        private Logger logger = LoggerFactory.getLogger(DepartmentDao.class);

        public void add(Department department) {
                try {
                        mapper.add(department);
                } catch (Exception e) {
                        logger.error(e.getMessage());
                }

        }


        /**
         * @exception "一定要循环删除部门的儿子"
         * @param id
         */
        public void delete(Integer id) {
                try {
                        //循环删除所有儿子
                        mapper.delete(id);//删除部门
                } catch (Exception e) {
                        logger.error(e.getMessage());
                }
        }
        public void delete(String oid) {
                try {
                        mapper.deleteByOid(oid);
                } catch (Exception e) {
                        logger.error(e.getMessage());
                }
        }

        public List<Integer> getSon(int id){
                try {
                        return mapper.getSon(id);
                }catch (Exception e){
                        logger.error(e.getMessage());
                        return null;
                }
        }

        public CopyOnWriteArrayList<Integer> getSons(int id){
                try {
                        return mapper.getSons(id);
                }catch (Exception e){
                        logger.error(e.getMessage());
                        return null;
                }
        }
        public void updateLogo(Department department) {
                try {
                        mapper.updateLogo(department);
                } catch (Exception e) {
                        logger.error(e.getMessage());
                }
        }

        public Department get(Integer id) {
                try {
                        return mapper.get(id);
                } catch (Exception e) {
                        logger.error(e.getMessage());
                        return null;
                }
        }

        public Department get(String oid,Integer id) {
                try {
                        return mapper.getByOidAndId(oid,id);
                } catch (Exception e) {
                        logger.error(e.getMessage());
                        return null;
                }
        }
        public List<Department> getByOid(String oid) {
                try {
                        return mapper.getByOid(oid);
                } catch (Exception e) {
                        logger.error(e.getMessage());
                        return null;
                }
        }
        public void update(Department department) {
                try {
                        mapper.update(department);
                } catch (Exception e) {
                        logger.error(e.getMessage());
                }
        }
        public Department getByOidAndDname(String oid,String name){
                try{
                        return mapper.getByOidAndDname(oid,name);
                }catch (Exception e){
                        logger.error(e.getMessage());
                        return null;
                }
        }


        public List<Department> getLeaf(String oid){
                try {
                        return mapper.getLeaf(oid);
                }catch (Exception e){
                        logger.error(e.getMessage());
                        return null;
                }
        }
        public Department getByOidAndNum(String oid,String memberNum){
                try {
                        return mapper.getByOidAndNum(oid,memberNum);
                }catch (Exception e){
                        logger.error(e.getMessage());
                        return null;
                }
        }


}
